﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Xml;
using System.Xml.Linq;

namespace Webservices
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AirportService.airportSoapClient webservice = new AirportService.airportSoapClient();

            if (Request.QueryString["city"] == null)
            {
                XDocument doc = XDocument.Parse(webservice.GetAirportInformationByCountry("Denmark"));
                var res = (from messages in doc.Descendants("Table")
                           select new ListItem()
                           {
                               Text = messages.Element("CityOrAirportName").Value,
                               Value = "Default.aspx?city=" + Server.UrlEncode(messages.Element("CityOrAirportName").Value)

                           }).Distinct();

                foreach (var li in res)
                {
                    uiCountryList.Items.Add(li);
                }

                if (Session["latitude"] != null) { 
                    Session.Remove("latitude");
                    Session.Remove("longitude");
                }
            }
            else
            {
                double longitude = 0.0d;
                double latitude = 0.0d;
                XDocument doc = XDocument.Parse(webservice.getAirportInformationByCityOrAirportName(Request.QueryString["city"]));
                //Page.Controls.Add(new LiteralControl(doc.ToString()));

                var elements = (from messages in doc.Descendants("Table")
                select new Coordination()
                           {
                               LongitudeDegree = double.Parse(messages.Element("LongitudeDegree").Value),
                               LongitudeMinute = double.Parse(messages.Element("LongitudeMinute").Value),
                               LongitudeSeconds = double.Parse(messages.Element("LongitudeSeconds").Value),
                               LatitudeDegree = double.Parse(messages.Element("LatitudeDegree").Value),
                               LatitudeMinute = double.Parse(messages.Element("LatitudeMinute").Value),
                               LatitudeSecond = double.Parse(messages.Element("LatitudeSecond").Value)


                           }).Distinct();

                foreach (Coordination c in elements) {
                    latitude = c.GetCoord(c.LatitudeDegree, c.LatitudeMinute, c.LatitudeSecond);
                    longitude = c.GetCoord(c.LongitudeDegree, c.LongitudeMinute, c.LongitudeSeconds);
                    
                    //Page.Controls.Add(new LiteralControl("<h1>" + latitude + "</h1>"));
                }

                //string url = "http://maps.google.com/?q=" + latitude.ToString("0.000000000000") + "," + longitude.ToString("0.000000000000");
                //Page.Controls.Add(new LiteralControl("<h1>" + url + "</h1>"));

                Session["latitude"] = latitude.ToString("0.000000000000");
                Session["longitude"] = longitude.ToString("0.000000000000");
            }

        }


    }
}